/*
 *  AMPProxyController.cpp
 *  AMPlugin
 *
 *  Created by Toan on 10/23/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include "VCPlugInHeaders.h"

// Interface includes:
#include "IActiveContext.h"
#include "ITextEditSuite.h" 
#include "ISelectionManager.h"
#include "IPanelControlData.h" 
#include "IDropDownListController.h"

// General includes:AMPProxyController
#include "CDialogController.h"
#include "StringUtils.h"
// Project includes:
#include <string>
#include "AMPID.h"
#include "HTTPRequestHelper.h"
#include "AMGlobal.h"
#include "IWidgetParent.h"
#include "ITextControlData.h"
#include "AMPLogDataRecord.h"
/** AMPProxyController
 Methods allow for the initialization, validation, and application of dialog widget
 values.
 Implements IDialogController based on the partial implementation CDialogController.
 
 
 @ingroup writefishprice
 */
class AMPProxyController : public CDialogController
	{
	public:
		/** Constructor.
		 @param boss interface ptr from boss object on which this interface is aggregated.
		 */
		AMPProxyController(IPMUnknown* boss) : CDialogController(boss) {}
		
		/** Destructor.
		 */
		virtual ~AMPProxyController() {}
		
		/** Initialize each widget in the dialog with its default value.
		 Called when the dialog is opened.
		 */
	    virtual void InitializeDialogFields(IActiveContext* dlgContext);
		
		/** Validate the values in the widgets.
		 By default, the widget with ID kOKButtonWidgetID causes
		 ValidateFields to be called. When all widgets are valid,
		 ApplyFields will be called.
		 @return kDefaultWidgetId if all widget values are valid, WidgetID of the widget to select otherwise.
		 
		 */
		virtual WidgetID ValidateDialogFields(IActiveContext* myContext);
		
		
		/** Retrieve the values from the widgets and act on them.
		 @param widgetId identifies the widget on which to act.
		 */
		virtual void ApplyDialogFields(IActiveContext* myContext, const WidgetID& widgetId);
	};

CREATE_PMINTERFACE(AMPProxyController, kAMPProxyControllerImpl)

/********************************************************************************************************************
 *****Function: InitializeDialogFields
 *****Creator:  Toan Nguyen
 *****IN: IActiveContext* dlgContext
 *****out: 
 *****Objective: init dialog
 ********************************************************************************************************************/
void AMPProxyController::InitializeDialogFields(IActiveContext* dlgContext)
{
	CDialogController::InitializeDialogFields(dlgContext);
	
	 PMString addressStr(g_ProxyServer.c_str());
	 PMString portStr(g_ProxyPort.c_str());
	 PMString usernameStr(g_ProxyUser.c_str());
	 PMString passwordStr(g_ProxyPass.c_str());
	 SetTextControlData(kAMPProxyIPWidgetID, addressStr);	//<--20120510
	 SetTextControlData(kAMPProxyPortWidgetID, portStr);
	 SetTextControlData(kAMPProxyUserWidgetID, usernameStr);
	 SetTextControlData(kAMPProxyPassWidgetID, passwordStr);

	do {
		InterfacePtr<IWidgetParent> iWidgetParent(this, UseDefaultIID());
		if(!iWidgetParent)
		{
			break;
		}
		InterfacePtr<IPanelControlData>	iPanelControlData( 
														  (IPanelControlData*)iWidgetParent->QueryParentFor(IID_IPANELCONTROLDATA) );
		if(!iPanelControlData)
		{
			break;
		}
		IControlView* clusterView = iPanelControlData->FindWidget(kAMPClusterProxyWidgetID);
		if(!clusterView)
		{
			break;
		}
		InterfacePtr<IPanelControlData> clusterPanel(clusterView, UseDefaultIID());
		if (!clusterPanel)
		{
			break;
		}
		IControlView* ManualMode = iPanelControlData->FindWidget(kAMPProxyGroupAllWidgetID);
		if(!ManualMode)
		{
			break;
		}
		
		if(g_ProxyEnable == FALSE)
		{
			//cout << "Set no proxy mode" << endl;
			IControlView* AutochildView = clusterPanel->FindWidget(kAMPRadioAutoWidgetID);
			if (AutochildView == nil)
			{
				break;
			}
			InterfacePtr<ITriStateControlData> autotriData(AutochildView, UseDefaultIID());
			if (autotriData)
			{
				autotriData->SetState(ITriStateControlData::kSelected, nil, nil);
			}
			IControlView* ManualchildView = clusterPanel->FindWidget(kAMPRadioManualWidgetID);
			if (ManualchildView == nil)
			{
				break;
			}
			InterfacePtr<ITriStateControlData> ManualtriData(ManualchildView, UseDefaultIID());
			if (ManualtriData)
			{
				ManualtriData->SetState(ITriStateControlData::kUnselected, nil, nil);
			}
			ManualMode->Disable();
			
		}else if (g_ProxyEnable == TRUE)
		{
			//cout << "Set proxy mode" << endl;
			IControlView* AutochildView = clusterPanel->FindWidget(kAMPRadioAutoWidgetID);
			if (AutochildView == nil)
			{
				break;
			}
			InterfacePtr<ITriStateControlData> autotriData(AutochildView, UseDefaultIID());
			if (autotriData)
			{
				autotriData->SetState(ITriStateControlData::kUnselected, nil, nil);
			}
			IControlView* ManualchildView = clusterPanel->FindWidget(kAMPRadioManualWidgetID);
			if (ManualchildView == nil)
			{
				break;
			}
			InterfacePtr<ITriStateControlData> ManualtriData(ManualchildView, UseDefaultIID());
			if (ManualtriData)
			{
				ManualtriData->SetState(ITriStateControlData::kSelected, nil, nil);
			}
			ManualMode->Enable(TRUE, TRUE);
			IControlView* UserPassEnable = iPanelControlData->FindWidget(kAMPProxyGroupPassWidgetID);
			if(!UserPassEnable)
			{
				break;
			}
			IControlView* CheckboxEnable = iPanelControlData->FindWidget(kAMPCheckboxEnablePassWidgetID);
			if(!CheckboxEnable)
			{
				break;
			}
			InterfacePtr<ITriStateControlData> checkboxtriData(CheckboxEnable, UseDefaultIID());
			if (!checkboxtriData)
			{
				break;
			}
			
			if(g_ProxyAuthEnable == TRUE)
			{
				//cout << "Set authen mode" << endl;
				UserPassEnable->Enable(TRUE, TRUE);
				checkboxtriData->SetState(ITriStateControlData::kSelected, nil, nil);
			}else
			{
				//cout << "No authen mode" << endl;
				UserPassEnable->Disable();
				checkboxtriData->SetState(ITriStateControlData::kUnselected, nil, nil);
			}
		}
		
	}while(kFalse);
}

/********************************************************************************************************************
 *****Function: ValidateDialogFields
 *****Creator:  Toan Nguyen
 *****IN: IActiveContext* myContext
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
WidgetID AMPProxyController::ValidateDialogFields(IActiveContext* myContext)
{
	WidgetID result = CDialogController::ValidateDialogFields(myContext);
	// Put code to validate widget values here.
	return result;
}

/********************************************************************************************************************
 *****Function: ApplyDialogFields
 *****Creator:  Toan Nguyen
 *****IN: IActiveContext* myContext, const WidgetID& widgetId
 *****out: 
 *****Objective: apply button is selected
 ********************************************************************************************************************/
void AMPProxyController::ApplyDialogFields(IActiveContext* myContext, const WidgetID& widgetId)
{
	// DropDownList result string.
	//cout << "AMPProxyController::ApplyDialogFields" << endl;
	//PMString resultString;

	PMString nUsernameString = this->GetTextControlData(kAMPProxyUserWidgetID);
	PMString nPasswordString = this->GetTextControlData(kAMPProxyPassWidgetID);
	PMString nDomainString = this->GetTextControlData(kAMPProxyIPWidgetID);
	PMString nPortString = this->GetTextControlData(kAMPProxyPortWidgetID);

	g_ProxyServer = nDomainString.GetPlatformString();
	g_ProxyPort = nPortString.GetPlatformString();
	g_ProxyUser = nUsernameString.GetPlatformString();
	g_ProxyPass = nPasswordString.GetPlatformString();
	g_ProxyAuthen = g_ProxyUser + ":" + g_ProxyPass;
	
	// save current setting
	SaveSettingInfo();
	//cout << "AMPProxyController::ApplyDialogFields" << endl;
}

//  Code generated by DollyXs code generator